﻿using System.Linq;

using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Infrastructure.AccessRights.Impl
{
    public partial class AccessRightsServiceImpl : IAccessRightsService
    {
        public bool CanUserAccess(IUser user, AccessRight accessRight)
        {
            var userRights = _userQueries.GetUserRights(user.Id);

            if(userRights == null) { return false; }

            if (userRights.Contains((int)AccessRight.IsAdmin)) { return true; }

            if (userRights.Contains((int)accessRight)) { return true; }
 
            return false;
        }

        public bool CanUserAccess(IUser user, int ownerId, AccessRight accessRight)
        {
            var userRights = _userQueries.GetUserRights(user.Id).ToList();

            if (userRights == null) { return false; }

            if (userRights.Contains((int)AccessRight.IsAdmin)) { return true; }

            if (user.Id == ownerId && userRights.Contains((int)accessRight)) { return true; }

            if (userRights.Contains((int)accessRight + 1)) { return true; }

            return false;
        }
    }

    public partial class AccessRightsServiceImpl
    {
        private readonly IUserQueries _userQueries;

        public AccessRightsServiceImpl(
            IUserQueries userQueries)
        {
            _userQueries = userQueries;
        }
    }
}
